import 'package:flutter/material.dart';
import '../../helper/Helper.dart';
import '../../model/hourdata.dart';

import '../../model/sum_day.dart';
import '../../model/sum_hour.dart';
import '../../model/swxg.dart';
import '../../model/xhnl.dart';

import '../../helper/channelHelper.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';

class HourDataListPage extends StatefulWidget {
  @override
  HourDataListPage();

  @override
  HourDataListPageState createState() => new HourDataListPageState();
}

class HourDataListPageState extends State<HourDataListPage> {
  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();

  SumHour? x1;
  bool ifOK = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    c2.text = Helper.DateTimeTodayString();
    c1.text = Helper.DateTimeYestodayString();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(title: Text("时段报表统计")),
      // floatingActionButton: getFloatingButton(context),
      body: HourDataListBody(context),
    );
  }

  void setQ() async {
    try {
      ifOK = false;
      x1 = await SumHour.Get(c1.text, c2.text);
      print(x1!.toJson());
      ifOK = true;
      this.setState(() => {});
      ChannelHelper.showToast("计算完成！");
    } catch (e) {
      ChannelHelper.showToast("计算错误！" + e.toString());
    }
  }

  String getStr(double xx) {
    return Helper.RoundDouble(xx, 1, " 万m3");
  }

  String getStr2(double xx) {
    return Helper.RoundDouble(xx, 2, " m");
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    list2.add(InputWidget.getInput("开始日期", c1));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getInput("结束日期", c2));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getButton("计算", setQ));

    list1.add(InputWidget.inCard2(list2));
    if (!ifOK) return list1;

    var showColor1 = Colors.blue;
    var vL = <Widget>[];

    vL.add(getShowItem("最大入库流量", x1!.MaxR, 0));
    vL.add(getShowItem("最小入库流量", x1!.MinR, 0));

    vL.add(getShowItem("最大出库流量", x1!.MaxQ, 0));
    vL.add(getShowItem("最小出库流量", x1!.MinQ, 0));

    vL.add(getShowItem("最高库水位", x1!.MaxH1, 2));
    vL.add(getShowItem("最低库水位", x1!.MinH1, 2));

    list3.add(InputWidget.getShowRow([vL[0], vL[1]], true));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getShowRow([vL[2], vL[3]], true));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getShowRow([vL[4], vL[5]], true));

    if (ifOK) {
      list1.add(InputWidget.inCard2(list3));

      list1.add(InputWidget.inCard(dayListGrid(x1!.HList)));
    }

    return list1;
  }

  Widget dayListGrid(List<HourData> data) {
    var x1 = <DataRow>[];
    var x2 = <DataRow>[];

    for (var a in data) {
      x1.add(DataRow(cells: [
        DataCell(Text(Helper.DateTimeFormat6(a.TM))),
      ]));

      x2.add(DataRow(cells: [
        DataCell(Text(Helper.RoundDouble(a.H1, 2, ""))),
        DataCell(Text(Helper.RoundDouble(a.H2, 2, ""))),
        DataCell(Text(Helper.RoundDouble(a.H3, 2, ""))),
        DataCell(Text(Helper.RoundDouble(a.FH1, 1, ""))),
        DataCell(Text(Helper.RoundDouble(a.FH1, 1, ""))),
        DataCell(Text(Helper.RoundDouble(a.FH3, 1, ""))),
        DataCell(Text(Helper.RoundDouble(a.FH4, 1, ""))),
        DataCell(Text(Helper.RoundDouble(a.E, 1, ""))),
        DataCell(Text(Helper.RoundDouble(a.FDLL1, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.FDLL2, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.FDLL3, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.FDLL4, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.QF, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.C, 2, ""))),
        DataCell(Text(Helper.RoundDouble(a.CSDLL, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.XHDLL1, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.XHDLL2, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.YHDLL, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.QP, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.QZ, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.R, 0, ""))),
      ]));
    }

    return Container(
        height: 400.0,
        child: SingleChildScrollView(
            child: Row(children: [
          Container(
              width: 140.0,
              child: DataTable(columns: [
                DataColumn(label: Text('日期')),
              ], rows: x1)),
          Expanded(
              child: SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: DataTable(columns: [
                    DataColumn(numeric: true, label: Text('水库水位')),
                    DataColumn(numeric: true, label: Text('尾水位')),
                    DataColumn(numeric: true, label: Text('发电水头')),
                    DataColumn(numeric: true, label: Text('1#机组负荷')),
                    DataColumn(numeric: true, label: Text('2#机组负荷')),
                    DataColumn(numeric: true, label: Text('3#机组负荷')),
                    DataColumn(numeric: true, label: Text('4#机组负荷')),
                    DataColumn(numeric: true, label: Text('总发电负荷')),
                    DataColumn(numeric: true, label: Text('1#机组流量')),
                    DataColumn(numeric: true, label: Text('2#机组流量')),
                    DataColumn(numeric: true, label: Text('3#机组流量')),
                    DataColumn(numeric: true, label: Text('4#机组流量')),
                    DataColumn(numeric: true, label: Text('总发电流量')),
                    DataColumn(numeric: true, label: Text('发电耗水率')),
                    DataColumn(numeric: true, label: Text('冲沙洞流量')),
                    DataColumn(numeric: true, label: Text('1#泄洪洞流量')),
                    DataColumn(numeric: true, label: Text('2#泄洪洞流量')),
                    DataColumn(numeric: true, label: Text('溢洪道流量')),
                    DataColumn(numeric: true, label: Text('总泄流量')),
                    DataColumn(numeric: true, label: Text('总出库流量')),
                    DataColumn(numeric: true, label: Text('入库流量')),
                  ], rows: x2)))
        ])));
  }

  Widget getShowItem(String title, double value, int h) {
    return InputWidget.getShowInfo(
        title, Helper.RoundDouble(value, h, ""), Colors.blue);
  }

  HourDataListBody(BuildContext context) => SingleChildScrollView(
        child: HourDataListFields(context),
      );

  HourDataListFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
